package com.glsc.ngateway.platform.service.it.networkAssets;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.networkassets.InterfaceStatus;
import com.glsc.ngateway.common.api.platform.dto.it.networkassets.InterfaceStatusDto;
import com.glsc.ngateway.common.api.common.dto.PageQueryParamDto;
import com.glsc.ngateway.common.base.converter.platform.InterfaceStatusConverter;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.networkAssets.InterfaceStatusRepo;
import com.glsc.ngateway.platform.utils.report.ReportFileConfigEnum;
import com.glsc.ngateway.platform.utils.report.ReportFileUtil;
import com.glsc.ngateway.platform.utils.report.ReportUploadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * 网络资产-接口状态
 */
@Service
public class InterfaceStatusService {
    private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceStatusService.class);

    @Autowired
    private InterfaceStatusRepo interfaceStatusRepo;

    /**
     * 查看
     */
    public Page<InterfaceStatusDto> query(PageQueryParamDto<InterfaceStatusDto> param) throws Exception {
        param.setParam(Objects.isNull(param.getParam()) ? new InterfaceStatusDto() : param.getParam());
        Pageable pageable = param.getPageable();
        Page<InterfaceStatus> pageRes = interfaceStatusRepo.page(pageable, param.getParam());
        return new PageImpl<>(InterfaceStatusConverter.do2Dto(pageRes.getContent()), pageable, pageRes.getTotalElements());
    }

    /**
     * 导出
     *
     * @param param
     * @return
     */
    public byte[] export(InterfaceStatusDto param) throws IOException {
        List<InterfaceStatusDto> datas = InterfaceStatusConverter.do2Dto(interfaceStatusRepo.queryAll(param));
        return ReportFileUtil.export(datas, ReportFileConfigEnum.INTERFACE_STATUS);
    }

    /**
     * 上传文件批量编辑
     */
    public ReportUploadContext<InterfaceStatusDto, String> upload(MultipartFile file) throws IOException {
        ReportUploadContext<InterfaceStatusDto, String> context = ReportUploadContext.<InterfaceStatusDto, String>builder()
                .config(ReportFileConfigEnum.INTERFACE_STATUS)
                .file(file)
                .build();
        ReportFileUtil.readSingleExcel(context);
        //全部检查通过后添加或修改数据
        if (context.isCheckPass()) {
            interfaceStatusRepo.saveAll(InterfaceStatusConverter.dto2Do(context.getReadRes()));
        }
        return context;
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(@NotNull Long id) {
        interfaceStatusRepo.deleteById(id);
    }

    /**
     * 添加/修改
     */
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdate(@NotNull InterfaceStatusDto interfaceStatusDto) {
        interfaceStatusRepo.save(InterfaceStatusConverter.dto2Do(interfaceStatusDto));
    }
}
